<?php  class export_employ extends Public_Controller{
	function __construct(){
		parent::__construct();		
		//$this->check_permissions();		
		$this->load->helper('form');
		$this->template->set_layout('salary.html');
		$this->excel_background = "";
		$this->load->helper("excel");
			$this->load->model("emp_room/export_employ_m",'export_employ');
		$this->list_excel = array(
			"users.display_name"=>"Họ Tên",
			"profile.sex"=>"Giới Tính",
			"profile.phone"=>"Số ĐT",
			"room.title"=>"Bộ Phận",
			"groups.description"=>"Chức vụ",
			"profile.birth"=>"Năm Sinh",
			"profile.location"=>"Chức Danh",
			"profile.address,profile.address2"=>"Địa chỉ",
			"profile.space"=>"Nơi Sinh",
			"profile.id_number,profile.id_number_date,profile.id_number_space"=>"CMND",
			"profile.literacy"=>"Trình độ học vấn",
			"profile.specialize"=>"Chuyên ngành",
			"profile.people"=>"Dân tộc",
			"profile.marry"=>"Tình trạng hôn nhân",
			"users.email"=>"Email",
			"profile.bank_id"=> "Tài Khoản Ngân Hàng",
			"profile.date_start"=>"Ngày vào làm");
		$this->list_basic = array(
			"users.display_name"=>"Họ Tên",
			"profile.birth"=>"Năm Sinh",
			"profile.sex"=>"Giới Tính",
			"profile.phone"=>"Số ĐT",
			"users.email"=>"Email",
		);
			
	}
	function index(){
		$this->action = 'back|'.site_url('emp_room/map').'|'.lang('global:back');
		$this->action .='+excel export_excel|javascript:void(0)|'.lang('excel:export_employ');
		$this->load->model("emp_profile/export_salary_m","export_m");
		if($this->current_user->group_id == 1){
			$room = $this->export_m->get_levelroom(1);
			$list_excel = $this->list_excel;
		}
		else{
			$id_room = $this->export_employ->get_room_em($this->current_user->id);
			$room = $this->export_m->get_levelroom("",$id_room["room_id"]);
			$list_excel = $this->list_basic;
		}
		$this->template
			->set('admin',true)->append_css('toolbar.css')
			->set("title",lang('salary:add_table_salary'))
			->set("room",$room)
			->set("check",$list_excel)
			->set("action",$this->action)
			->append_css("breadcrumb.css")
			->append_css('table.css')
			->append_css('ui/jquery-ui.min.css')
			->append_css('chosen/chosen.css')
			->append_js('ui/jquery-ui.min.js')
			->append_js('filter.js')
			->append_js('admin/plugins.js')
			->append_js('admin/scripts.js')
			->append_js('business/site.js')
			->append_js('chosen/chosen.jquery.min.js')
			->build($this->controller."/index");	
	}
	function export_excel(){
		$post = $this->input->get();
		if($this->current_user->group_id == 1) $list_excel = $this->list_excel;else $list_excel = $this->list_basic;
		if(empty($post["room_listid"])){
			$this->load->model("emp_profile/export_salary_m","export_m");
			if($this->current_user->group_id == 1){
				$room = $this->export_m->get_levelroom(1);
			}
			else{
				$id_room = $this->export_employ->get_room_em($this->current_user->id);
				$room = $this->export_m->get_levelroom("",$id_room["room_id"]);
			}
			if(!empty($room)) foreach($room as $item){
				$post["room_listid"][] = $item->id;
			}
		}
		$post["table"] = (!empty($post["table"]))?$post["table"]:"";
		$data_employ = $this->export_employ->get_room($post["room_listid"],$post["table"],$list_excel); // danh sach nhan vien
		/* xuat file excel */
		$this->load->library("excel");
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->setActiveSheetIndex(0);
		$symbol_width = array(); // chieu rong từng cot	
		$list_title	= array();
		foreach($this->list_excel as $value => $name){
			if(!empty($post["table"])){
				if(in_array($value,$post["table"])){
					$list_title[] =  $name;
				}
			}
			else
				$list_title[] =  $name;
		}
		$array_tabledata  = array_intersect($list_excel,$list_title);
		
		$list_title = array();
		$list_title = $this->list_title($post["table"],$list_excel);
		$symbol_width = array();
		header_excel($list_title,$symbol_width,$objPHPExcel,1,false); // xuat title
		$this->data_excel($data_employ,$array_tabledata,$objPHPExcel); // insert du lieu vao excel
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="Danh_sach_nhan_vien.xls"');
		header('Cache-Control: max-age=0'); 
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5'); 
		$objWriter->save('php://output');
	}
	function list_title($post,$list_excel)
	{
		$post["table"]	=	$post;
		foreach($list_excel as $value => $name){
			$array_value = array_filter(explode(",",$value));
			if(!empty($post["table"])){
				if(in_array($value,$post["table"])){
					if(count($array_value)>1){
						if($value == "profile.address,profile.address2"){
							$list_title[] = "Thường trú";
							$list_title[] = "Tam Trú";
						}
						elseif($value == "profile.id_number,profile.id_number_date,profile.id_number_space"){
							$list_title[] = "Số CMND";
							$list_title[] = "Ngày cấp";
							$list_title[] = "Nơi cấp";
						}
					}
					else
						$list_title[] =  $name;
				}
			}	
			else{
					if(count($array_value)>1){
						if($value == "profile.address,profile.address2"){
							$list_title[] = "Thường trú";
							$list_title[] = "Tam Trú";
						}
						elseif($value == "profile.id_number,profile.id_number_date,profile.id_number_space"){
							$list_title[] = "Số CMND";
							$list_title[] = "Ngày cấp";
							$list_title[] = "Nơi cấp";
						}
					}
					else
						$list_title[] =  $name;
			}
		}
		return $list_title;	
	}
	function data_excel($data_employ,$array_tabledata,$objPHPExcel)
	{
		if(!empty($data_employ)){
			$row=1;
			//print_r($data_employ);exit;
			foreach($data_employ as  $item){
				$row++;
				$col=0;
				foreach($array_tabledata as $value => $name){
					$array_value = array_filter(explode(",",$value));
					if(count($array_value)>1){
						for($j=0;$j<count($array_value);$j++){
							$_value = substr(strstr($array_value[$j],"."),1);
							if($_value == "id_number")
								$item[$_value] = " ".$item[$_value];
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,$item[$_value]);
							$col++;
						}	
					}
					else{
						$value = substr(strstr($value,"."),1);
						if($value ==  "sex")
							$item[$value] = ($item[$value] == 0)?"Nữ":"Nam";
						elseif($value == "phone")
							$item[$value] = " ".$item[$value];
						elseif($value == "literacy"){
						
						if($item[$value] == 1)
							$item[$value] = "Đại học";
						elseif($item[$value] == 2)
							$item[$value] = "Cao Đẳng";
						elseif($item[$value] == 3)
							$item[$value] = "Trung cấp";
						elseif($item[$value] == 5)
							$item[$value] = "Sau Đại Học";
						elseif($item[$value] == 4 || ($item[$value] == 0))
							$item[$value] = "Phổ Thông";
						}
						if($value == "marry")
							$item[$value] = ($item[$value] == 0)?"Độc thân":"Đã có gia đình";
						if($value == "bank_id")	
							$item[$value] = " ".$item[$value];
						if($value == "date_start")
							$item[$value] = date("d/m/Y",$item[$value]);
						if($value == "birth")
							$item[$value] = date("d/m/Y",$item[$value]);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,$item[$value]);
						$col++;
					}
				}
			}
		}	
	}
}